package training.project.app.repository.impl;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import training.project.app.domain.FlightClient;
import training.project.app.domain.UserAccount;
import training.project.app.repository.UserDao;
import training.project.app.repository.base.AbstractHibernateDao;

@Repository
@Transactional
public class UserRepository extends AbstractHibernateDao<UserAccount, Integer>
		implements UserDao
	{
		@Override
		public UserAccount findById(Integer userId)
			{
				Criteria cr = getSession().createCriteria(UserAccount.class,
						"User").add(Restrictions.eq("id", userId));
				return (UserAccount) cr.uniqueResult();
			}

		public UserAccount findByLogin(String login)
			{
				Criteria cr = getSession().createCriteria(UserAccount.class,
						"User").add(Restrictions.eq("login", login));
				return (UserAccount) cr.uniqueResult();
			}

		@Override
		public void purchaseFlight(UserAccount user,
				FlightClient flightClient)
			{
				user.setMoney(user.getMoney() - flightClient.getPrice());
				System.out.println(user.getMoney());
			}
	}
